package model;

import java.util.ArrayList;
import java.util.concurrent.Semaphore;

import controller.Controller;

public abstract class Player {
	protected ArrayList<Card> cards = new ArrayList<Card>();
	protected int playerID;
	private int points = 0;
	protected int bid = 0;
	protected int win = 0;
	protected String playerName;
	private boolean playerTurn = false;
	private boolean isNextPlayer = false;
	protected ArrayList<Card> allCalledCards = new ArrayList<Card>();
	private Card selectedCard = null;
	
	public Player(int playerID,String playerName) {
		this.playerID = playerID;
		this.playerName = playerName;
	}
	
	public int getPlayerID() {
		return playerID;
	}
	
	public int getNumberOfCards() {
		return cards.size();
	}
	
	public void receiveACard(Card card) {
		cards.add(card);
	}
	
	public void addToAllCalledCards(Card card) {
		allCalledCards.add(card);
	}
	
	public void removeCard(Card card) {
		card.setPlayer(-1);
		cards.remove(card);
	}
	
	public abstract Card askForACard(ArrayList<Card> nextPlayerCard, int thPlayer, Colors trumpColor);
	
	public abstract int askForABid(int playersNumber, Colors trumpColor);
	
	public void addWin() {
		win++;
	}
	
	public void processRoundData() {
		
		if (bid == win)
			points = points + 10 + 2*win;
		else
			points = points - 2*Math.abs(win-bid);
		
		allCalledCards.removeAll(allCalledCards);
		
		win = 0;
		bid = -1;
	}
	
	public int getPoints(){
		return points;
	}
	
	public void setBid(int bid) {
		this.bid = bid;
	}

	public String getName() {
		return playerName;
	}

	public int getBid() {
		return bid;
	}
	
	public int getWin() {
		return win;
	}

	public int getCardID(Card card) {
		for(int i = 0; i < cards.size(); ++i) {
			if (card == cards.get(i))
				return i;
		}
		return -1;
	}

	public void setController(Controller controller) {
		return;
	}

	public void setSemaphore(Semaphore semaphore) {
		return;
	}

	public boolean hasColor(Colors color) {
		for(int i = 0; i < cards.size(); ++i)
			if (cards.get(i).getColor() == color)
				return true;
		return false;
	}

	public boolean isPlayerTurn() {
		return playerTurn;
	}
	
	public boolean isNextPlayer() {
		return isNextPlayer;
	}
	
	public void setSelectedCard(int cardIndex) {
		if (cardIndex == -1) {
			for(int i = 0; i < cards.size(); ++i)
				cards.get(i).setSelected(false);
		}
		else
			cards.get(cardIndex).setSelected(true);
	}
	
	public Card getSelectedCard() {
		return selectedCard;
	}
	
	public void setPlayerTurn(boolean turn, boolean next) {
		playerTurn = turn;
		isNextPlayer = next;
	}
}